<!DOCTYPE html>
<html lang="en">
<#if cashcardInfo??>
    <title>编辑储值卡兑换码</title>
    <#else>
        <title>新增储值卡兑换码</title>
</#if>
<#import "/layout/layout.html" as body/>
<@body.page>

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title overflow">
                     <#if cashcardInfo??>
                        <h5 class="pull-left">编辑储值卡兑换码</h5>
                     <#else>
                    	<h5 class="pull-left">新增储值卡兑换码</h5>
                    </#if>
                    
                </div>
                <div class="ibox-content">
                    <form method="get" class="form-horizontal" id="addForm">
                        <input type="hidden" id="id" name="id" value="${(cashcardInfo.id)!}">
                        <input type="hidden" id="cashcardStatus" name="cashcardStatus" value="${(cashcardInfo.cashcardStatus)!}">
                        <input type="hidden" id="version" name="version" value="${(cashcardInfo.version)!}">
                        <#if cashcardInfo??>

                        <#else>
                            <div class="form-group"><h2 class="col-sm-12 margin-top">Step1:配置基础信息</h2></div>
                        </#if>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group"><label class="col-sm-2 control-label">领取方式</label>
                            <div class="col-sm-10" id="get_cashcard">
                                <div class="checkbox-inline i-checks"><label for="is_user"> <input type="radio" id="is_user" value="1" name="getMode"> <i></i> 用户输码领取 </label></div>
                                <div class="checkbox-inline i-checks"><label for="is_system"> <input type="radio" id="is_system" value="2" checked name="getMode"> <i></i> 系统发送 (新增后获取ID，由研发配置，用户领取的券码为随机) </label></div>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">券码</label>
                            <div class="col-sm-10">
                                <input type="text" style="display: none;" id="cashcardCode" name="cashcardCode" value="${(cashcardInfo.cashcardCode)!}" class="form-control" placeholder="请输入用户领取储值卡券的兑换码，如：HelloLemon">
                                <div id="randomCode" style="margin-top: 8px">券码为随机码</div>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">储值卡券名称</label>
                            <div class="col-sm-10">
                                <input type="text" id="cashcardName" name="cashcardName" value="${(cashcardInfo.cashcardName)!}" class="form-control" placeholder="请输入企业用户名称或用户名称">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group"><label class="col-sm-2 control-label">生成数量</label>
                            <div class="col-sm-10"><input type="number" min="0" id="maxCount" name="maxCount" value="${(cashcardInfo.maxCount)!}" class="form-control" placeholder="如无限量则填写“0”，如有限量则填写对应数量"></div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group"><label class="col-sm-2 control-label">发放金额</label>
                            <div class="col-sm-10"><input type="number" id="cashcardAmt" name="cashcardAmt" value="${(cashcardInfo.cashcardAmt)!}" class="form-control" placeholder="请输入单个兑换码对应的储值卡金额"></div>
                        </div>
                         <div class="form-group get_isOneVote" id="get_isOneVote" ><label class="col-sm-2 control-label">兑换数量</label>
                            <div class="col-sm-10" >
                            	<#if cashcardInfo??>
                            		<div class="checkbox-inline i-checks"><label for="isOneVote1"> <input type="radio" id="isOneVote1" value="0"  name="isOneVote" <#if cashcardInfo.isOneVote==0>checked</#if>> <i></i>每人可兑换多个码 </label></div>
                                	<div class="checkbox-inline i-checks"><label for="isOneVote2"> <input type="radio" id="isOneVote2" value="1" name="isOneVote" <#if cashcardInfo.isOneVote==1>checked</#if>> <i></i>每人限制兑换一个码</label></div>
                            	<#else>
                            		<div class="checkbox-inline i-checks"><label for="isOneVote1"> <input type="radio" id="isOneVote1" value="0" checked name="isOneVote"> <i></i>每人可兑换多个码</label></div>
                                	<div class="checkbox-inline i-checks"><label for="isOneVote2"> <input type="radio" id="isOneVote2" value="1"  name="isOneVote"> <i></i>每人限制兑换一个码</label></div>
                            	</#if>
                                
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group"><label class="col-sm-2 control-label">是否设置有效期</label>
                            <div class="col-sm-10" id="has_time">
                            	<#if cashcardInfo??>
                            		<div class="checkbox-inline i-checks"><label for="cash_reel"> <input type="radio" id="cash_reel" value="1" name="changeMode"   <#if cashcardInfo.changeMode==1>checked</#if>  > <i></i> 永久有效 </label></div>
                                	<div class="checkbox-inline i-checks"><label for="vip_status"> <input type="radio" id="vip_status" value="2" name="changeMode" <#if cashcardInfo.changeMode==2>checked</#if>  > <i></i> 设置有效期 </label></div>
                            	<#else>	
                            		<div class="checkbox-inline i-checks"><label for="cash_reel"> <input type="radio" id="cash_reel" value="1" name="changeMode"> <i></i> 永久有效 </label></div>
                                	<div class="checkbox-inline i-checks"><label for="vip_status"> <input type="radio" id="vip_status" value="2" name="changeMode" checked> <i></i> 设置有效期 </label></div>
                            	</#if>
                                
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group time_limit">
                            <label class="col-sm-2 control-label">兑换有效期</label>
                            <div class="col-sm-10">
                                <div class="col-sm-6 padding-left-0">
                                    <div class="input-group">
                                        <input id="changeBeginDate" name="changeBeginDate" class="form-control" size="16" type="text" placeholder="兑换生效日期" readonly>
                                        <span class="input-group-addon"><span class="fa fa-clock-o"></span></span>
                                    </div>
                                </div>
                                <div class="col-sm-6 padding-right-0">
                                    <div class="input-group date form_datetime">
                                        <input id="changeEndDate" name="changeEndDate" class="form-control" size="16" type="text" placeholder="兑换失效日期" readonly>
                                        <span class="input-group-addon"><span class="fa fa-clock-o"></span></span>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="hr-line-dashed time_limit"></div>
                        <div class="form-group"><label class="col-sm-2 control-label">备注</label>
                            <div class="col-sm-10"><input type="text" id="remark" name="remark" value="${(cashcardInfo.remark)!}" class="form-control" placeholder="非必填，可输入相关备注信息"></div>
                        </div>
                  
                        <div class="hr-line-dashed show-store"></div>
                    </form>
                </div>
                <div class="ibox-content overflow">
                    <div class="col-sm-12">
                        <button type="button" id="go_back_list" class="btn btn-default pull-left">取消添加</button>

                        <#if cashcardInfo?? && cashcardInfo.cashcardStatus ==2>
                            <a id="cashcard_update" href="#" class="btn btn-primary pull-right">更新</a>
                            <#else>
                                <a id="cashcard_add_step2" href="#" class="btn btn-primary pull-right">保存并进入下一步</a>
                        </#if>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="${baseUrl}js/main.js"></script>
<link href="${baseUrl}css/plugins/clockpicker/clockpicker.css" rel="stylesheet">
<script src="${baseUrl}js/plugins/clockpicker/clockpicker.js"></script>
<script>
	//返回列表
	$('#go_back_list').click(function () {
	    window.location = "to_cashcard_list";
	});
	
	//更新
	$('#cashcard_update').click(function () {
	    save();
	});
	
	//保存进入第2步
	$('#cashcard_add_step2').click(function () {
	    save();
	});
	
	function save() {
	
	        var isCheck = true;
//	        debugger
	        if($('input[name="getMode"]:checked').val()==1 && $("#cashcardCode").val().trim().length <=0){
	            swal("错误!","请先输入券码","error");
	            isCheck = false;
	            return false;
	        }
	        if($("#cashcardName").val().trim().length <=0){
	            swal("错误!","储值卡券名称","error");
	            isCheck = false;
	            return false;
	        }
	        if($("#maxCount").val().length <=0){
	            swal("错误!","请先输入发送数量","error");
	            isCheck = false;
	            return false;
	        }
	        if($("#cashcardAmt").val().length <=0){
	            swal("错误!","请先输入储值卡金额","error");
	            isCheck = false;
	            return false;
	        }

	        if($("input[name='changeMode']:checked").val()==2 && ($("#changeBeginDate").val().length <=0 ||$("#changeEndDate").val().length <=0 )  ){
	            swal("错误!","请先选择兑换有效期","error");
	            isCheck = false;
	            return false;
	        }

	
	        if (!isCheck){
	            return false;
	        }
	        var param = $("#addForm").serialize();
	        $.post("save_cashcard_add_step1",param,function (result) {
	            result = JSON.parse(result);
	            debugger
	            if(result && result.go == "list"){
	                swal("更新成功!", "页面即将重新加载,请稍后...", "success");
	                window.setTimeout(function () {
	                    window.location = '${baseUrl}system/cashcard/to_cashcard_list'
	                }, 1000);
	            } else if(result.success) {
	                swal("保存成功!", "页面即将重新加载,请稍后...", "success");
	                window.setTimeout(function () {
	                    window.location = '${baseUrl}system/cashcard/to_add_step2/' + result.id
	                }, 1000);
	            } else {
	                swal("保存失败!", result.msg, "error");
	            }
	        });
	
	
	    }
	
	
	
	$(document).ready(function () {
	    //初始化日期格式
	    Date.prototype.pattern = function (fmt) {
	        var o = {
	            "M+": this.getMonth() + 1, //月份
	            "d+": this.getDate(), //日
	            "h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时
	            "H+": this.getHours(), //小时
	            "m+": this.getMinutes(), //分
	            "s+": this.getSeconds(), //秒
	            "q+": Math.floor((this.getMonth() + 3) / 3), //季度
	            "S": this.getMilliseconds() //毫秒
	        };
	        var week = {
	            "0": "/u65e5",
	            "1": "/u4e00",
	            "2": "/u4e8c",
	            "3": "/u4e09",
	            "4": "/u56db",
	            "5": "/u4e94",
	            "6": "/u516d"
	        };
	        if (/(y+)/.test(fmt)) {
	            fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	        }
	        if (/(E+)/.test(fmt)) {
	            fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "/u661f/u671f" : "/u5468") : "") + week[this.getDay() + ""]);
	        }
	        for (var k in o) {
	            if (new RegExp("(" + k + ")").test(fmt)) {
	                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	            }
	        }
	        var str = fmt.lastIndexOf("N");
	        if (str != -1) {
	            return "";
	        }
	
	        return fmt;
	    }
	
	    //初始化数据
	    var item = ${string!'null'};
	    if(item){
	        //判断是否审核完成 审核完成只可修改2条属性
	        if(item.cashcardStatus ==2){
	            $("input[type='radio'],[type='checkbox'],#cashcardCode,#maxCount,#cashcardAmt,#changeBeginDate,#changeEndDate").attr("disabled", true);
	        }
	
	
	        if(item.getMode ==1){
	            $('#cashcardCode').show();
	            $('#randomCode').hide();
	        }
	        if(item.getMode ==2){
	            $('#cashcardCode').hide();
	            $('#randomCode').show();
	        }
	        if(item.getMode && item.getMode == 1){
	            $("#is_user").prop('checked', true);
	        }
	        if(item.getMode && item.getMode == 2){
	            $("#is_system").prop('checked', true);
	        }
	        if(item.changeMode && item.changeMode == 1){
	            //兑换类型 不限制
	            $("#no_time").prop('checked', true);
	        }
	        if(item.changeMode && item.changeMode == 2) {
	            //兑换类型 指定日期
	            $("#has_time").prop('checked', true);
	            $("#changeBeginDate").val(new Date(item.changeBeginDate).pattern('yyyy-MM-dd'));
	            $("#changeEndDate").val(new Date(item.changeEndDate).pattern('yyyy-MM-dd'));
	
	        }
	        if(item.changeMode == 1){
	            $('.time_limit').hide();
	        }

	        debugger
	        if(item.changeBeginDate){
	            //兑换有效期 初始化值
	            $('#changeBeginDate').daterangepicker({
	                startDate: "${(cashcardInfo.changeBeginDate?string('yyyy-MM-dd'))!}",
	                endDate: "${(cashcardInfo.changeEndDate?string('yyyy-MM-dd'))!}",
	                minDate: new Date(),//'01/01/2016',    //最小时间new Date(),//
	                //maxDate: 0,//moment(), //最大时间
	                dateLimit: {days: 60}, //起止时间的最大间隔
	                showWeekNumbers: false, //是否显示第几周
	                //timePicker: true, //是否显示小时和分钟
	                //timePicker12Hour: false, //是否使用12小时制来显示时间
	                timePickerIncrement: 10,
	                opens: 'right', //日期选择框的弹出位置
	                buttonClasses: ['btn btn-default'],
	                applyClass: 'btn-small btn-primary blue',
	                cancelClass: 'btn-small',
	                format: 'YYYY-MM-DD', //控件中from和to 显示的日期格式 YYYY-MM-DD HH:mm:ss
	                separator: '~',
	                locale: {
	                    applyLabel: '确定',
	                    cancelLabel: '取消',
	                    fromLabel: '兑换生效日期',
	                    toLabel: '兑换失效日期',
	                    customRangeLabel: '自定义',
	                    daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
	                    monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
	                    firstDay: 1
	                }
	            });
	
	            $('#changeBeginDate').on('apply.daterangepicker', function (ev, picker) {
	                //解决input中开始结束时间被不正确放到了一个input中的问题
	                var timeArr = ev.currentTarget.value.split("~");
	                $('#changeBeginDate').val(timeArr[0].substring(0, 10));
	                $('#changeEndDate').val(timeArr[1].substring(0, 10));
	            });
	        } else {
	            debugger;
	            console.log("兑换有效期 不初始化值");
	            //兑换有效期 不初始化值
	            $('#changeBeginDate').daterangepicker({
	                minDate: new Date(),//'01/01/2016',    //最小时间new Date(),//
	                //maxDate: 0,//moment(), //最大时间
	                dateLimit: {days: 60}, //起止时间的最大间隔
	                showWeekNumbers: false, //是否显示第几周
	                //timePicker: true, //是否显示小时和分钟
	                //timePicker12Hour: false, //是否使用12小时制来显示时间
	                timePickerIncrement: 10,
	                opens: 'right', //日期选择框的弹出位置
	                buttonClasses: ['btn btn-default'],
	                applyClass: 'btn-small btn-primary blue',
	                cancelClass: 'btn-small',
	                format: 'YYYY-MM-DD', //控件中from和to 显示的日期格式 YYYY-MM-DD HH:mm:ss
	                separator: '~',
	                locale: {
	                    applyLabel: '确定',
	                    cancelLabel: '取消',
	                    fromLabel: '兑换生效日期',
	                    toLabel: '兑换失效日期',
	                    customRangeLabel: '自定义',
	                    daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
	                    monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
	                    firstDay: 1
	                }
	            });
	
	            $('#changeBeginDate').on('apply.daterangepicker', function (ev, picker) {
	                //解决input中开始结束时间被不正确放到了一个input中的问题
	                var timeArr = ev.currentTarget.value.split("~");
	                $('#changeBeginDate').val(timeArr[0].substring(0, 10));
	                $('#changeEndDate').val(timeArr[1].substring(0, 10));
	            });
	        }

	
	
	    } else{
	        debugger;
	        console.log("兑换有效期 不初始化值");
	        //兑换有效期 不初始化值
	        $('#changeBeginDate').daterangepicker({
	            minDate: new Date(),//'01/01/2016',    //最小时间new Date(),//
	            //maxDate: 0,//moment(), //最大时间
	            dateLimit: {days: 60}, //起止时间的最大间隔
	            showWeekNumbers: false, //是否显示第几周
	            //timePicker: true, //是否显示小时和分钟
	            //timePicker12Hour: false, //是否使用12小时制来显示时间
	            timePickerIncrement: 10,
	            opens: 'right', //日期选择框的弹出位置
	            buttonClasses: ['btn btn-default'],
	            applyClass: 'btn-small btn-primary blue',
	            cancelClass: 'btn-small',
	            format: 'YYYY-MM-DD', //控件中from和to 显示的日期格式 YYYY-MM-DD HH:mm:ss
	            separator: '~',
	            locale: {
	                applyLabel: '确定',
	                cancelLabel: '取消',
	                fromLabel: '兑换生效日期',
	                toLabel: '兑换失效日期',
	                customRangeLabel: '自定义',
	                daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
	                monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
	                firstDay: 1
	            }
	        });
	
	        $('#changeBeginDate').on('apply.daterangepicker', function (ev, picker) {
	            //解决input中开始结束时间被不正确放到了一个input中的问题
	            var timeArr = ev.currentTarget.value.split("~");
	            $('#changeBeginDate').val(timeArr[0].substring(0, 10));
	            $('#changeEndDate').val(timeArr[1].substring(0, 10));
	        });
	        console.log("使用有效期 不初始化值");
	        //使用有效期 不初始化值
	       
	    }
	
	
	    //radio 选择
	    $('.i-checks').iCheck({
	        checkboxClass: 'icheckbox_square-green',
	        radioClass: 'iradio_square-green',
	    });
	
	
	
	
	
	    //是否 系统发送
	    $('#get_cashcard input').on('ifChecked', function () {
	        if ($(this).val() === '2') {
	            $('#cashcardCode').hide();
	            $('#randomCode').show();
                $("#isOneVote1,#isOneVote2").attr("disabled", false).parents('.iradio_square-green').removeClass('disabled');
                $("#isOneVote1").attr("checked", true).parents('.iradio_square-green').addClass('checked');
                $("#isOneVote2").attr("checked", false).parents('.iradio_square-green').removeClass('checked');
	        } else {
	            $("#isOneVote1,#isOneVote2").attr("disabled", true).parents('.iradio_square-green').addClass('disabled');
                $("#isOneVote1").attr("checked", false).parents('.iradio_square-green').removeClass('checked');
                $("#isOneVote2").attr("checked", true).parents('.iradio_square-green').addClass('checked');
	            $('#cashcardCode').show();
	            $('#randomCode').hide();
	        }
	    });
	
	    //兑换有效期类型
	    $('#has_time input').on('ifChecked', function () {
	        if ($(this).val() === '2') {
	            $('.time_limit').show(400);
	        } else {
	            $('.time_limit').hide(400);
	        }
	    });
	    
	   
	
	});
</script>
</@body.page>
</html>